/**
 *  Copyright 2010 Kyle O'Connor, Justin Arling, Mike Mercieca
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package edu.drexel.sd0910.ece01.aqmon.util;

import org.apache.log4j.Logger;

import edu.drexel.sd0910.ece01.aqmon.data.DylosData;

/**
 * Utils class that provides miscellaneous methods related to data manipulation.
 * 
 * @author Kyle O'Connor
 * 
 */
public class DataUtils {

	private final static Logger log = Logger.getLogger(DataUtils.class);

	/**
	 * Converts Particle Counts (per 0.1 ft^3) for the PM10 range to Mass<br>
	 * References Lee and Titarelli papers.
	 * 
	 * @param largeParticleCount
	 *            PM10 (large) count
	 * @return <code>double</code> value of Mass (per m^3)
	 */
	public static double convertPC10toMass(int largeParticleCount) {
		double r10 = 2.6 * Math.pow(10.0, -6.0); // um
		double vol10 = (4.0 / 3.0) * Math.PI * Math.pow(r10, 3.0);

		double density = 1.65 * Math.pow(10.0, 12.0); // ug/m3

		double mass10 = density * vol10; // ug

		// dylos output = particles/.01 ft^3
		// 35.315 ft^3 = 1 m^3
		// 35.315/.01=3531.5/m^3

		double K = 3531.5; // per m^3

		// PM10 concentration (particle count - sum of small and large
		// concentrations)
		double concLarge = (largeParticleCount) * K * mass10; // ug/m^3

		return concLarge;
	}

	/**
	 * Converts Particle Counts (per 0.1 ft^3) for the PM2.5 range to Mass<br>
	 * References Lee and Titarelli papers.
	 * 
	 * @param smallParticleCount
	 *            PM2.5 (small) count
	 * @return <code>double</code> value of Mass (per m^3)
	 */
	public static double convertPC25toMass(int smallParticleCount) {
		double r25 = 0.44 * Math.pow(10.0, -6.0); // um
		double vol25 = (4.0 / 3.0) * Math.PI * Math.pow(r25, 3.0);

		double density = 1.65 * Math.pow(10.0, 12.0); // ug/m3

		double mass25 = density * vol25; // ug

		// dylos output = particles/.01 ft^3
		// 35.315 ft^3 = 1 m^3
		// 35.315/.01=3531.5/m^3

		double K = 3531.5; // per m^3

		// PM2.5 concentration (particle count from channel 1)
		double conc25First = smallParticleCount * K * mass25; // ug/m^3

		return conc25First;
	}

	/**
	 * Takes a DylosData object and outputs a String of flot formatted data.
	 * 
	 * @param data
	 *            DylosData object reference
	 * @return String of flot formatted data
	 */
	@Deprecated
	public static String generateFlotFormattedData(DylosData data) {
		StringBuffer temp = new StringBuffer();
		temp.append("{");
		temp.append("label: '" + data.getNodeID() + "',data: [");
		temp.append("[" + data.getDate().getTime() + "," + data.getParticleCounts()[0] + "]");
		temp.append("]");
		temp.append("}");

		log.debug(temp.toString());

		return temp.toString();
	}

	/**
	 * Static utils class not to be instantiated.
	 */
	private DataUtils() { }

}
